package com.kaibes.platform.article.api;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.platform.article.db.entity.ArticleComment;
import com.kaibes.platform.article.db.service.ArticleCommentService;
import com.kaibes.platform.article.db.service.ArticleService;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/articles/comments")
public class ArticleCommentApi extends ResponseApi<ArticleComment, ArticleCommentService> {
    
    @Autowired
    private ArticleService articleService;
    
    @PostMapping
    @ApiLink(value = "创建评论", validated = false)
    public ResponseEntity<List<Number>> post(HttpServletRequest request, @RequestAttribute Integer userId, @RequestBody @Validated ArticleComment data) {
        if (articleService.isCommentAble(data.getArticleId())) {
            data.setUserId(userId);
            data.setCreateTime(LocalDateTime.now());
            return super.post(data, "发表评论成功", "发表评论失败");
        } else {
            return ResponseFactory.postFailure("当前文章禁止评论");
        }
    }
    
    @DeleteMapping("/{id}")
    @ApiLink("删除评论，根据id")
    public ResponseEntity<Object> deleteById(@PathVariable BigInteger id) {
        return super.deleteById(id, "删除评论成功", "删除评论失败");
    }
    
    @DeleteMapping
    @ApiLink("删除评论，根据id")
    public ResponseEntity<Object> deleteByIdQuery(@RequestBody List<BigInteger> ids) {
        return super.deleteByIds(ids, "删除评论成功", "删除评论失败");
    }
    
    @DeleteMapping("/self/{id}")
    @ApiLink("删除自己或者所属帖子的评论，根据id")
    public ResponseEntity<Object> deleteById(@RequestAttribute Integer userId, @PathVariable BigInteger id) {
        ArticleComment target = service.getUserIdArticleIdById(id);
        if (userId.intValue() != target.getUserId().intValue()) {
            BigInteger articleId = target.getArticleId();
            Integer author = articleService.getUserIdById(articleId);
            if (userId.intValue() != author.intValue()) {
                return ResponseFactory.putFailure("不能删除他人的评论！");
            }
        }
        return super.deleteById(id, "删除评论成功", "删除评论失败");
    }
    
    @DeleteMapping("/self")
    @ApiLink("删除自己或者所属帖子的评论，根据id")
    public ResponseEntity<Object> deleteByIdQuery(@RequestAttribute Integer userId, @RequestBody List<BigInteger> ids) {
        for (BigInteger id : ids) {
            ArticleComment target = service.getUserIdArticleIdById(id);
            if (userId.intValue() != target.getUserId().intValue()) {
                BigInteger articleId = target.getArticleId();
                Integer author = articleService.getUserIdById(articleId);
                if (userId.intValue() != author.intValue()) {
                    return ResponseFactory.putFailure("不能删除他人的评论！");
                }
            }
        }
        return super.deleteByIds(ids, "删除评论成功", "删除评论失败");
    }
    
}
